//
//  ST_NoticeSettingViewController.swift
//  btvideo
//
//  Created by mac on 2021/1/30.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import NIMSDK

class ST_NoticeSettingViewController: ST_BTLightBarViewController {
    
    init(accountAdditionalAttributes:ST_BTAccountAdditionalAttribute) {
        self.attributes = accountAdditionalAttributes
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    let attributes:ST_BTAccountAdditionalAttribute
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title_nav_btn.setTitleForAllStates("新消息通知")
        
         
        
        self.view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: statusBarHeight+49, left: 0, bottom: 0, right: 0))
        }
        
        
        scrollView.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalToSuperview().offset(10)
        }
        
        root_view.addSubview(item0)
        root_view.addSubview(item1)
        root_view.addSubview(item2)
        root_view.addSubview(item3)
        
        
        item0.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(5)
            make.height.equalTo(61)
        }
        
        item1.snp.makeConstraints { (make) in
            make.left.right.height.equalTo(item0)
            make.top.equalTo(item0.snp.bottom)
        }
        
        
        item2.snp.makeConstraints { (make) in
            make.left.right.height.equalTo(item0)
            make.top.equalTo(item1.snp.bottom)
        }
        
        item3.snp.makeConstraints { (make) in
            make.left.right.height.equalTo(item0)
            make.top.equalTo(item2.snp.bottom)
        }
        
    }
    
    
    
    
    //    MARK: - Properties
    fileprivate lazy var scrollView:UIScrollView = {
        let control = UIScrollView()
        control.backgroundColor = .white
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.showsVerticalScrollIndicator = false
        return control
    }()
    
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        return control
    }()
    
    fileprivate lazy var item0:SettingSwitchCell = {
        let control = SettingSwitchCell()
        
        var m = SettingSwitchCellModel.init(title: "通知显示消息详情", isOn: false)
        
        if let setting = NIMSDK.shared().apnsManager.currentSetting(),setting.type == .detail {
            m.isOn = true
        }
        
        control.model = m
        
        control._switchClosure = {[weak self] isOn in
            self?.updateApnsSetting(showDetail: isOn)
        }
        
        return control
    }()
    
    fileprivate lazy var item1:SettingSwitchCell = {
        let control = SettingSwitchCell()
        let m = SettingSwitchCellModel.init(title: "收到点赞消息提醒", isOn: attributes.allowThumbUpNotification)
        control.model = m
        control._switchClosure = {[weak self] isOn in
            self?.SettingThumbup(isOn: isOn)
        }
        return control
    }()
    
    fileprivate lazy var item2:SettingSwitchCell = {
        let control = SettingSwitchCell()
        let m = SettingSwitchCellModel.init(title: "收到评论消息提醒", isOn: attributes.allowRemarkNotification)
        control.model = m
        control._switchClosure = {[weak self] isOn in
            self?.SettingComment(isOn: isOn)
        }
        return control
    }()
    
    fileprivate lazy var item3:SettingSwitchCell = {
        let control = SettingSwitchCell()
        let m = SettingSwitchCellModel.init(title: "收到喜欢消息提醒", isOn: attributes.allowLoveNotification)
        control.model = m
        control._switchClosure = {[weak self] isOn in
            self?.SettingReceiveLove(isOn: isOn)
        }
        return control
    }()
    
    
}


extension ST_NoticeSettingViewController{
    
    fileprivate func updateApnsSetting(showDetail:Bool){
        
        let setting = NIMPushNotificationSetting.init()
        
        if showDetail{
            setting.type = .detail
        }else{
            setting.type = .noDetail
        }
        
        NIMSDK.shared().apnsManager.updateApnsSetting(setting) { (err) in
            
            guard let e = err else{
                return
            }
            self.toast(text: e.localizedDescription)
            
        }
    }
    
    
    fileprivate func SettingThumbup(isOn:Bool){
        
        
        pushNoticeSetting(key: "switch_thumb_up", value: isOn)
        
    }
    
    fileprivate func SettingComment(isOn:Bool){
        
        
        pushNoticeSetting(key: "switch_comment", value: isOn)
        
    }
    
    fileprivate func SettingReceiveLove(isOn:Bool){
        
        pushNoticeSetting(key: "switch_receive_like", value: isOn)
        
    }
    
    
    fileprivate func pushNoticeSetting(key:String,value:Bool){
        
        
        JVrequest(PH.settingNoticeApi,parameters: [key:value ? 1 : 2]) { (json) in
            
            switch key{
            case "switch_thumb_up":
                self.attributes.allowThumbUpNotification = value
            case "switch_comment":
                self.attributes.allowRemarkNotification = value
            case "switch_receive_like":
                self.attributes.allowLoveNotification = value
            default:
                break
            }
            
            
        } failure: { (code, err) in
            
            self.toast(text: err)
        }

        
    }
    
    
    
    
}

//MARK: - Cell
class SettingSwitchCell: UICollectionViewCell {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(info_label)
        
        info_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25)
            make.centerY.equalToSuperview()
        }
        contentView.addSubview(select_control)
        select_control.snp.makeConstraints { (make) in
            make.centerY.equalTo(info_label)
            make.right.equalToSuperview().offset(-20)
        }
        
        let line = UIView()
        line.backgroundColor = UIColor.init(hex: 0xF2F4F7)
        contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalTo(info_label).offset(-5)
            make.height.equalTo(1)
            make.right.bottom.equalToSuperview()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var info_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor.init(hexString: "#333333")
        //        control.numberOfLines = 0
        control.textAlignment = .left
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var select_control:UISwitch = {
        let control = UISwitch()
        control.onTintColor = UIColor.init(hex: 0xFEC207)
        control.backgroundColor = UIColor.init(hex: 0xACB4C0)
        control.cornerRadius = 16
        control.addTarget(self, action: #selector(_switch_handle), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    var _switchClosure:((Bool)->())?
    
    @objc func _switch_handle(){
        
        _switchClosure?(select_control.isOn)
        
    }
    
    var model:SettingSwitchCellModel?{
        didSet{
            
            guard let m = model else {
                return
            }
            
            info_label.text = m.title
            select_control.isOn = m.isOn
        }
    }
    
}


//MARK: - Model
struct SettingSwitchCellModel {
    var title:String
    var isOn:Bool
}
